package com.pet.serve.repository;

import com.pet.serve.entity.Collect;
import com.pet.serve.entity.Collect.collectType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.Modifying;

import java.util.List;

public interface CollectRepository extends JpaRepository<Collect, Long> {

    // 根据用户名查询所有收藏，支持分页
    List<Collect> findByUsername(String username);

    // 根据用户名和收藏类型查询
    List<Collect> findByUsernameAndType(String username, collectType type);

    // 根据用户名和商品编号删除收藏
    @Modifying
    @Query("DELETE FROM Collect c WHERE c.username = :username AND c.petid = :goodid")
    void deleteByUsernameAndGoodid(@Param("username") String username, @Param("goodid") Long goodid);

    @Modifying
    @Query("DELETE FROM Collect c WHERE c.username = :username AND c.petid = :petid")
    void deleteByUsernameAndPetid(@Param("username") String username, @Param("petid") Long petid);

    // 根据用户名、收藏类型、状态查询
    List<Collect> findByUsernameAndTypeAndStatus(String username, collectType type, String status);

    // 查询用户收藏的所有有效（非下架）的商品或宠物
    @Query("SELECT c FROM Collect c WHERE c.username = :username AND c.status = 'active'")
    List<Collect> findActiveCollectsByUsername(@Param("username") String username);
}
